
package com.kosugi.flickingcoin;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import com.badlogic.gdx.graphics.GL10;

/**
 * A vertex shaded cube.
 */
class Ellipse extends Shape
{
	
	final int vCircleSize = 20;
	
    public Ellipse() {
        //float one = (float) 0x10000;
    	mRad = 1f;
        updateGeometry();
    }
    
    public void setRad(float rad) {
    	if(mRad == rad)
    		return;

    	mRad = rad;
    	updateGeometry();
	}
    
    @Override
    public boolean isHitted(float hx, float hy) {
    	float dist2 = (hx - pos[0]) * (hx - pos[0]) + (hy - pos[1]) * (hy - pos[1]);
    	if (dist2 < mRad * mRad)
    		return true;
    	else
    		return false;
    }
    
    public void updateGeometry() {
    	
    	mRad = 1f;
    	
    	float pi = 3.14159f;
        /*
        float one = 1f;
        
        float vertices[] = {
                -one, -one, 0,
                one, -one, 0,
                one,  one, 0,
                -one,  one, 0,
        };
        */
    	
    	
    	int vSize = vCircleSize + 1;
        
        float vertices[] = new float[vSize*3];
        
        vertices[0] = 0f;
        vertices[1] = 0f;
        vertices[2] = 0f;
        
        for (int i = 0; i < vCircleSize; i++) {
        	vertices[3 + i*3] = mRad * (float)Math.cos(2*pi / 20 * i);
        	vertices[3 + i*3+1] = mRad * (float)Math.sin(2*pi / 20 * i);
        	vertices[3 + i*3+2] = 0f;
        }
        
        /*
        one = 1f;

        float colors[] = {
                0,    0,    0,  one,
                one,    0,    0,  one,
                one,  one,    0,  one,
                0,  one,    0,  one,
        };
        */
        
        float colors[] = new float[vSize*4];
        
        for (int i = 0; i < 21; i++) {
            colors[i*4] = color[0];
            colors[i*4+1] = color[1];
            colors[i*4+2] = color[2];
            colors[i*4+3] = alpha;
        
        }
        /*
        byte indices[] = {
                0, 1, 2,   0, 2, 3
        };
        */
        
        
        
        byte indices[] = new byte[vCircleSize*3];
        
        int i;
        for(i = 0; i < vCircleSize -1; i++) {
        	indices[i*3] = 0;
        	indices[i*3 + 1] = (byte)(i + 1);
        	indices[i*3 + 2] = (byte)(i + 2);
        }
        //
        
        indices[ i * 3] = 0;
        indices[ i * 3 + 1] = (byte)(i + 1);
        indices[ i * 3 + 2] = (byte)(1);
        
        

        // Buffers to be passed to gl*Pointer() functions
        // must be direct, i.e., they must be placed on the
        // native heap where the garbage collector cannot
        // move them.
        //
        // Buffers with multi-byte datatypes (e.g., short, int, float)
        // must have their byte order set to native order

        ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length*4);
        vbb.order(ByteOrder.nativeOrder());
        mVertexBuffer = vbb.asFloatBuffer();
        mVertexBuffer.put(vertices);
        mVertexBuffer.position(0);

        ByteBuffer cbb = ByteBuffer.allocateDirect(colors.length*4);
        cbb.order(ByteOrder.nativeOrder());
        mColorBuffer = cbb.asFloatBuffer();
        mColorBuffer.put(colors);
        mColorBuffer.position(0);

        mIndexBuffer = ByteBuffer.allocateDirect(indices.length);
        mIndexBuffer.put(indices);
        mIndexBuffer.position(0);
    }

    public void drawSelf(GL10 gl)
    {
        gl.glFrontFace(GL10.GL_CCW);
        gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mVertexBuffer);
        gl.glColorPointer(4, GL10.GL_FLOAT, 0, mColorBuffer);
        gl.glDrawElements(GL10.GL_TRIANGLES, vCircleSize*3, GL10.GL_UNSIGNED_BYTE, mIndexBuffer);
    }

    private FloatBuffer   mVertexBuffer;
    private FloatBuffer   mColorBuffer;
    private ByteBuffer  mIndexBuffer;
    
    private float mRad;
}
